package com.bxait.toymail.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Chen
 * @description: es服务的配置文件
 * @ClassName ToyMailElasticSearchConfig
 * @create 2023-04-14 21:06
 * @Version 1.0
 */
@Configuration
public class ToyMailElasticSearchConfig {
    public static final RequestOptions COMMON_OPTIONS;
    static {
        // 自定义RequestOptions
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        // 可以在配置上放一下请求头等信息
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    public RestHighLevelClient esRestClient(@Value("${spring.elasticsearch.jest.uris}")String esUrl){
        //TODO 修改为线上的地址
        RestClientBuilder builder = null;
        builder = RestClient.builder(HttpHost.create(esUrl));
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }
}
